home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / progargslib.lha / ProgArgs / Examples / example1.c < prev    next >
C/C++ Source or Header  |  1995-04-08  |  10KB  |  345 lines

  1.  
  2. #include <proto/graphics.h>
  3. #include <proto/gadtools.h>
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6. #include <proto/intuition.h>
  7. #include <proto/utility.h>
  8. #include <proto/asl.h>
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #ifndef LINKLIB
  15.     #include "proto/progargs.h"
  16. #else
  17.     #include "libraries/progargs.h"
  18.     #include "clib/progargs_protos.h"
  19. #endif
  20.  
  21. #ifdef DEBUGMODULE
  22.     
  23.     #include "support/debug.h"
  24.  
  25. #else
  26.  
  27.     void __stdargs kprintf(UBYTE *fmt,...);  // Serial debugging...
  28.     void __stdargs dprintf(UBYTE *fmt,...);  // Parallel debugging...
  29.  
  30.     #ifndef bug
  31.     #define bug Printf
  32.     #endif
  33.  
  34.     #ifndef DEBTIME
  35.     #define DEBTIME 0
  36.     #endif
  37.  
  38.     #ifdef DEBUG
  39.     #define D(x) (x); if(DEBTIME>0) Delay(DEBTIME);
  40.     #else
  41.     #define D(x) ;
  42.     #endif
  43.  
  44. #endif
  45.  
  46. #define MIN_VERSION     37L             // minimum version number for our libs
  47.  
  48. long __oslibversion = MIN_VERSION;
  49. char __stdiowin[] = "CON:0/10/640/200/ProgArgs Example 1";
  50.  
  51. #ifndef LINKLIB
  52. struct Library *ProgArgsBase = NULL;
  53. #endif
  54.  
  55. static void quit(UBYTE *err);
  56. static void closedown(void);
  57.  
  58. /*********************************************
  59.  **
  60.  ** Routines for a clean exit, with optional error display
  61.  **
  62.  *********************************************/
  63.  
  64. static struct EasyStruct error_es = {
  65.     sizeof(struct EasyStruct), 0,
  66.     "ProgArgs Example1 Requester",
  67.     "Problem during startup:\n%ls",
  68.     "Quit"
  69. };
  70.  
  71. static void quit(UBYTE *err)
  72. {
  73.     closedown();
  74.  
  75.     if(err != NULL) (void)EasyRequest(NULL,&error_es, NULL, err);
  76.  
  77.     _exit(0);
  78. }
  79.  
  80. static void closedown(void)
  81. {
  82.     #ifndef LINKLIB
  83.     if(ProgArgsBase) CloseLibrary(ProgArgsBase);
  84.     #endif
  85. }
  86.  
  87. /*********************************************
  88.  **
  89.  ** Identifiers for program arguments
  90.  **
  91.  *********************************************/
  92.  
  93. enum {
  94.     ARG_String = (int)TAG_ARGENTRY+1,
  95.     ARG_Number,
  96.     ARG_Switch,
  97.     ARG_Toggle,
  98.     ARG_DefStr,
  99.     ARG_DefNum,
  100.     ARG_Multi,
  101.     ARG_Clear
  102. };
  103.  
  104. /*********************************************
  105.  **
  106.  ** Default values for a multi-argument type.
  107.  ** (Name), (Directory lock), (Reserved)
  108.  **
  109.  *********************************************/
  110.  
  111. struct MultiArg DefMulti[] = {
  112.     { "Multi1", NULL, NULL },
  113.     { "Multi2", NULL, NULL },
  114.     { "Multi3", NULL, NULL },
  115.     { "Multi4", NULL, NULL },
  116.     { NULL }
  117. };
  118.  
  119. /*********************************************
  120.  **
  121.  ** Definitions of all program arguments.
  122.  ** (ArgID), (Template), (Description),
  123.  ** (Flags), (Default value), (Reserved)
  124.  **
  125.  *********************************************/
  126.  
  127. struct ArgEntry Keywords[] = {
  128.     { ARG_String, "STRING=STR/K", "Just a silly generic string",
  129.         0, NULL, NULL },
  130.         
  131.     { ARG_Number, "NUMBER=NUM/K/N", "A number, still generic",
  132.         AEFLAG_HEXNUM, NULL, NULL },
  133.         
  134.     { ARG_Switch, "SWITCH=SW/K/S", "A basic switch",
  135.         0, 0, NULL },
  136.         
  137.     { ARG_Toggle, "TOGGLE=TOG/K/T", "A toggle, click click click",
  138.         AEFLAG_DEFAULT, 0, NULL },
  139.         
  140.     { ARG_DefStr, "DEFSTR=DSTR/K", "A string, but with a default",
  141.         AEFLAG_DEFAULT, "DefString", NULL },
  142.         
  143.     { ARG_DefNum, "DEFNUM=DNUM/K/N", "A number that knows what it likes",
  144.         AEFLAG_DEFAULT, (APTR)100, NULL },
  145.         
  146.     { ARG_Multi, "MULTI=MUL/M", "Expandable, flexible, ta-da!",
  147.         AEFLAG_DEFAULT, &DefMulti[0], NULL },
  148.         
  149.     { ARG_Clear, "CLEAR=CLR/K/S", "Clear to default values",
  150.         0, 0, NULL },
  151.         
  152.     { TAG_END }
  153. };
  154.  
  155. /*********************************************
  156.  **
  157.  ** Print the values of all our argument IDs.
  158.  ** [This should be made generic, but I'm lazy. ;)]
  159.  **
  160.  *********************************************/
  161.  
  162. void print_args(struct ProgArgs* pa)
  163. {
  164.     ULONG val;
  165.  
  166.     printf("  ARG_String = ");
  167.     if(GetProgArgs(pa,ARG_String,&val,TAG_END)) printf("\"%ls\"\n",val);
  168.     else printf("<nothing>\n");
  169.  
  170.     printf("  ARG_Number = ");
  171.     if(GetProgArgs(pa,ARG_Number,&val,TAG_END)) printf("%ld\n",val);
  172.     else printf("<nothing>\n");
  173.  
  174.     printf("  ARG_Switch = ");
  175.     if(GetProgArgs(pa,ARG_Switch,&val,TAG_END)) printf("%ld\n",val);
  176.     else printf("<nothing>\n");
  177.  
  178.     printf("  ARG_Toggle = ");
  179.     if(GetProgArgs(pa,ARG_Toggle,&val,TAG_END)) printf("%ld\n",val);
  180.     else printf("<nothing>\n");
  181.  
  182.     printf("  ARG_DefStr = ");
  183.     if(GetProgArgs(pa,ARG_DefStr,&val,TAG_END)) printf("\"%ls\"\n",val);
  184.     else printf("<nothing>\n");
  185.  
  186.     printf("  ARG_DefNum = ");
  187.     if(GetProgArgs(pa,ARG_DefNum,&val,TAG_END)) printf("%ld\n",val);
  188.     else printf("<nothing>\n");
  189.  
  190.     printf("  ARG_Clear = ");
  191.     if(GetProgArgs(pa,ARG_Clear,&val,TAG_END)) {
  192.         if(val) printf("%ld => Will write empty arguments.\n",val);
  193.         else printf("%ld => Will write current values.\n",val);
  194.     } else printf("<nothing>\n");
  195.  
  196.     printf("  ARG_Multi = ");
  197.     if(GetProgArgs(pa,ARG_Multi,&val,TAG_END)) {
  198.         struct MultiArg* multi = (struct MultiArg*)val;
  199.         int i;
  200.         
  201.         printf("%lx\n",multi);
  202.         i=0;
  203.         while(multi->ma_String) {
  204.             UBYTE buffer[512];
  205.             printf("  #%d = (%ls)\n",i,multi->ma_String);
  206.             NameFromLock(multi->ma_Directory,&buffer[0],500);
  207.             printf("    CD = (%ls), Ext = %lx\n",&buffer[0],multi->ma_Ext);
  208.             multi++;
  209.             i++;
  210.         }
  211.     }
  212.     else printf("<nothing>\n");
  213. }
  214.  
  215. extern struct WBStartup *_WBenchMsg;
  216.  
  217. void __stdargs __main(char *line)
  218. {
  219.     struct ProgArgs* args;
  220.     struct ProgArgs* file;
  221.     UBYTE name[512];
  222.     
  223. #ifndef LINKLIB
  224.     ProgArgsBase = OpenLibrary("progargs.library", 0);
  225.     if (!ProgArgsBase)
  226.        quit("Can't open progargs.library.");
  227.     D(bug("Opened the library.  Testing...\n",NULL));
  228. #endif
  229.     
  230.     printf("-------------------------------------------------------------\n");
  231.     printf("Initial program info\n");
  232.     printf("-------------------------------------------------------------\n");
  233.     printf("  cmdline   = %ls\n",line);
  234.     printf("  arguments = %ls\n",GetArgStr());
  235.  
  236.     GetProgramName(&name[0],500);
  237.     printf("  prog name = %ls\n",&name[0]);
  238.     
  239.     #if 0
  240.     {
  241.         struct CSource cs;
  242.         cs.CS_Buffer = line;
  243.         cs.CS_Length = strlen(line);
  244.         cs.CS_CurChr = 0;
  245.         ReadItem(&name[0],500,&cs);
  246.         printf("arg1 = %ls\n",&name[0]);
  247.     }
  248.     #endif
  249.     
  250.     #if 0
  251.     strcpy(&name[0],"KEYWORD");
  252.     for(i=0;i<19;i++) {
  253.         for(j=0;j<6;j++) {
  254.             printf("%ls ",&name[0]);
  255.             next_sequence(&name[0]);
  256.         }
  257.         printf("\n");
  258.     }
  259.     #endif
  260.     
  261.     printf("\n-------------------------------------------------------------\n");
  262.     printf("Basic parse of command/workbench arguments\n");
  263.     printf("-------------------------------------------------------------\n");
  264.  
  265.     args = AllocProgArgs(&Keywords[0],
  266.                          PA_WBStartup,      _WBenchMsg,     // Hand in WB startup
  267.                          RPA_WBArguments,   ARG_Multi,      // Parse WB startup
  268.                          PA_IgnoreError,    TRUE,           // Don't fail on next err
  269.                          RPA_ProgIcon,      !_WBenchMsg,    // If no WB startup,
  270.                                                             // read directly from icon
  271.                          PA_IgnoreError,    FALSE,          // Allow fail on next err
  272.                          RPA_CmdInput,      TRUE,           // Parse command line
  273.                          TAG_END);
  274.  
  275.     if(!args) {
  276.         quit("Unable to parse arguments.\n");
  277.     }
  278.     
  279.     printf("  new arguments = %ls\n",GetArgStr());
  280.  
  281.     printf("  Parsed arguments:\n");
  282.     printf("  ~~~~~~~~~~~~~~~~\n");
  283.     print_args(args);
  284.  
  285.     printf("\n-------------------------------------------------------------\n");
  286.     printf("Parse of a text file, modified by command/workbench arguments\n");
  287.     printf("-------------------------------------------------------------\n");
  288.  
  289.     file = AllocProgArgs(&Keywords[0],TAG_END);
  290.  
  291.     if(!file) {
  292.         FreeProgArgs(args);
  293.         quit("Unable to open file ProgArgs.\n");
  294.         exit(20);
  295.     }
  296.     
  297.     if( !ExecProgArgs(file,
  298.                       PA_SetDirectory,  PADIR_CURRENT,  // Move to current dir
  299.                       PA_IgnoreError,   TRUE,           // Ignore any errors.
  300.                       RPA_TextFileName, "TestArgsData.txt", // Try to read file
  301.                       RPA_CmdLine,      GetArgStr(),    // Parse cmd line, without
  302.                                                         // any interaction
  303.                       TAG_END) ) {
  304.         printf("==> Unable to read file.\n");
  305.     }
  306.     
  307.     printf("  Parsed arguments:\n");
  308.     printf("  ~~~~~~~~~~~~~~~~\n");
  309.     print_args(file);
  310.     
  311.     printf("\nWriting basic arguments to program icon...\n");
  312.     if( !ExecProgArgs(args,
  313.                        PA_AllComments, GetTagData(ARG_Clear,FALSE,args->pa_Arguments),
  314.                        WPA_ProgIcon, TRUE,
  315.                        TAG_END) ) printf("==> Error!\n");
  316.     
  317.     printf("Writing text file \"TestArgsData.txt\" to disk...\n");
  318.     if( !ExecProgArgs(file,
  319.                       PA_SetDirectory,  PADIR_CURRENT,
  320.                       WPA_TextFileName, "TestArgsData.txt",
  321.                       TAG_END) ) printf("==> Error!\n");
  322.     
  323.     printf("Writing defaults file \"TestDefsData.txt\" to disk...\n");
  324.     if( !ExecProgArgs(file,
  325.                       PA_SetDirectory,  PADIR_CURRENT,
  326.                       PA_AllComments,   TRUE,
  327.                       WPA_TextFileName, "TestDefsData.txt",
  328.                       TAG_END) ) printf("==> Error!\n");
  329.     
  330.     printf("Writing text file \"ENV:TestDir/TestArgsData.txt\" to\n");
  331.     printf("disk and to archive directory...\n");
  332.     if( !ExecProgArgs(file,
  333.                       PA_SetDirectory,  PADIR_ENVARC,
  334.                       PA_CreateDirs,    TRUE,
  335.                       WPA_TextFileName, "TestDir/TestArgsData.txt",
  336.                       TAG_END) ) printf("==> Error!\n");
  337.     
  338.     FreeProgArgs(file);
  339.     FreeProgArgs(args);
  340.     
  341.     printf("Done.\n");
  342.     quit(NULL);
  343. }
  344.  
  345.